/* 
    生成基础题目
    返回一个对象
*/
function creatTest() {
    let arg1 = getRandom(1, 100);
    let arg2 = getRandom(1, 100);
    let operator; //运算符
    let flag = getRandom(1, 4);
    let res2;
    // console.log(flag);

    if (flag === 1) {
        operator = ' + '
        res2 = arg1 + arg2
    } else if (flag === 2) {
        operator = ' - '
        res2 = arg1 - arg2
    } else if (flag === 3) {
        operator = ' * '
        res2 = arg1 * arg2
    } else if (flag === 4) {
        operator = ' / '
        res2 = (arg1 / arg2).toFixed(2);
    }
    // console.log(res2,'res2');
    res2 = res2.toString()

    let testStr = arg1 + operator + arg2;

    return {
        arg1,
        arg2,
        operator,
        testStr,
        res1: '', //用户的答案
        res2, //真正的答案,
        isTrue: false
    }
}

/* 
    生成进阶题目
    返回对象和上一个相似
*/
function creatTest1() {
    let arg1 = getRandom(1, 100);
    let arg2 = getRandom(1, 100);
    let arg3 = getRandom(1, 100);
    let operator1 = getOperator(getRandom(1, 4)); //运算符
    let operator2 = getOperator(getRandom(1, 4))
    let testStr
    let res2;
    if (operator1 === operator2 && (operator1 === ' + ' || operator1 === ' * ')) {
        testStr = `${arg1}${operator1}${arg2}${operator2}${arg3}`;
    } else {
        if (getRandom(1, 2) === 1) {
            testStr = `(${arg1}${operator1}${arg2})${operator2}${arg3}`;
        } else {
            testStr = `${arg1}${operator1}(${arg2}${operator2}${arg3})`;
        }
    }

    res2 = getRes(arg1,arg2,arg3,operator1,operator2,testStr);
    return {
        testStr,
        res1: '', //用户的答案
        res2, //真正的答案,
        isTrue: false
    }
}
/* 
    生成随机数
    a - b之间的 整数
*/
function getRandom(a, b) {
    return Math.round(Math.random() * (b - a) + a)
}

/* 
    生成随机运算符
*/
function getOperator(arg) {
    let operator
    if (arg === 1) {
        operator = ' + '
    } else if (arg === 2) {
        operator = ' - '
    } else if (arg === 3) {
        operator = ' * '
    } else if (arg === 4) {
        operator = ' / '
    }
    return operator
}

/* 
    计算高级运算的结果
*/
function getRes(arg1,arg2,arg3,operator1,operator2,testStr) {
    let res;
    let a;
    if(testStr.indexOf('(') === 0){
        a = calRes(arg1,arg2,operator1)
        console.log(a,'111');
        res = calRes(a,arg3,operator2)
    }else{
        a = calRes(arg2,arg3,operator2)
        console.log(a,'222');
        res = calRes(arg1,a,operator1)
    }
    return res = res.toString()
}
/* 
    计算两个操作数 一个运算符号的结果
*/
function calRes(arg1,arg2,oper){
    let res;
    // console.log(arg1,arg2,oper,'555');
    if (oper === ' + ') {
        res = arg1 + arg2
    } else if (oper === ' - ') {
        res = arg1 - arg2
    } else if (oper === ' * ') {
        res = (arg1 * arg2)
    } else if (oper === ' / ') {
        res = (arg1 / arg2).toFixed(2);
    }
    // console.log(res,'555');
    return res;
}

export {
    creatTest,
    creatTest1
}